package com.patri.tema1;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class myDialog extends Activity{
	EditText et;
	Button ok,cancel;
	TextView tv;
	String g;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.dialog);
	    
	    tv=(TextView)findViewById(R.id.tv);
	    et=(EditText)findViewById(R.id.etv);
	    ok=(Button)findViewById(R.id.ok);
	    cancel=(Button)findViewById(R.id.cancel);
	    
	    ok.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int k=0;
				  try {
					  URL url= new URL("http://www.google.com/ig/api?weather="+et.getText().toString().toLowerCase());
					  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
						DocumentBuilder db = dbf.newDocumentBuilder();
						Document doc = db.parse(new InputSource(url.openStream()));
						//Document doc = db.parse(fXmlFile);
						doc.getDocumentElement().normalize();
						if (doc.getElementsByTagName("problem_cause") != null) 
							{ k=1;}
				  }catch(Exception e) {
    		e.printStackTrace();
    	  }
				  if (k==1)  
				  {g =et.getText().toString();
				  
				  }
				  else g="";
				Intent intent = new Intent();
				//intent.setClass(myDialog.this, Main.class);
				intent.putExtra("oras", g);
				  
				//startActivity(intent);
				setResult(1,intent);
				finish();
				   
			}
		});
cancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(myDialog.this, Main.class);
				
				startActivity(intent);
				
			}
		});
	    
	}
}
